/*
 * Copyright (c) 2025, 廖树财, <study_2030@163.com>
 *
 * PES1.0.3-YCYW is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2     
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
#pragma once
#include "define.h"
#include "JsonParser.h"
#include "zlog.h"
#include <errno.h>
#include "http.h"
#include "upload.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

extern sqlite3 *g_sqlite3_db;
typedef struct stru_IOTA_APP_DOWNLOAD
{
    char *fileName;
    // char *fileLink;
    char *fileId;
    char *accessToken;
    char *digestAlgorithm;
    char *digest;
    char *fileType;
    int size;
} IOTA_APP_DOWNLOAD;

typedef struct stru_IOTA_APP_DOWNLOAD_PROGRESS
{
    char *fileId;
} IOTA_APP_DOWNLOAD_PROGRESS;

typedef struct stru_IOTA_APP_DOWNLOAD_PROGRESS_RES
{
    char *appId;
    char *filename;
    long totalSize;
    long size;
} IOTA_APP_DOWNLOAD_PROGRESS_RES;

typedef struct stru_IOTA_APP_CTRL
{
    char *appId;
    char *appOldId;
    char *fileId;
    char *containerId;
    char *containerName;
    char *containerHyperv;
    char *appLocated;
    char *appName;
    char *appVersion;
} IOTA_APP_CTRL;

typedef struct stru_IOTA_APP_ADD_NEEDINFO
{
    char *appId;
    char *appName;
    char *version;
    int isTrustworthy;
} IOTA_APP_ADD_NEEDINFO;

typedef struct stru_IOTA_APP_CTRL_RES
{
    char *appId;
    char *appOldId;
} IOTA_APP_CTRL_RES;

typedef struct stru_APP_INFO
{
    char appid[50];
    char appversion[50];
    int isTrustworthy;
    char name[64];
} APP_INFO;

typedef struct stru_CONTAINER_INFO
{
    char type[50];
    char containerid[50];
    char id[32];
} CONTAINER_INFO;

bool SqlAddAppInfo(IOTA_APP_CTRL param, char *filename);

bool SqlAddAppInfo_V1(IOTA_APP_ADD_NEEDINFO param);

bool SqlAddAppInfo_V2(IOTA_APP_CTRL param, char* filename);

bool SqlDeleteAppInfo(IOTA_APP_CTRL param, char *filename);

bool SqlDeleteAppInfoByAppName(IOTA_APP_CTRL param, char *filename);

bool SqlSearchAppid(APP_INFO *app, char *appname);

bool SqlSearchAppidByVer(char *appid, char *appversion);

int SqlSearchContainerid(CONTAINER_INFO *container, char *containername);

bool SqlSearchAppInfo(char *appid, char *appname);

bool SqlDeleteApp(char *appid);

bool JsonToStruct_AppDownload(char *pcJson, ST_IOTA_LOG *pcStatusDecs);

bool JsonToStruct_AppDownloadProgress(char *pcJson, ST_IOTA_LOG *pcStatusDecs);

bool JsonToStruct_AppCtrl(char *pcJson, ST_IOTA_LOG *pcStatusDecs);

bool StructToJson_AppDownLoadRes(char **pcJson, ST_IOTA_LOG_RES *pcStatusDecs);

bool StructToJson_AppDownLoadProgressRes(char **pcJson, ST_IOTA_LOG_RES *pcStatusDecs);

int AppDownLoad(char *pJson);

int terminalAppDownLoad(char *pJson);

int app_commond(char *pcJson);

